package com.ruoyi.project.app.util;
import static org.junit.Assert.assertEquals;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


import com.alibaba.fastjson.JSONObject;
import org.junit.Test;


public class HttpTest {
	private static String account = "mt6327";//mt3569
	private static String password = "mgvpx";
	
	@Test
	public void sendSms() {
		int respCode=0;
		long t1 = System.currentTimeMillis();
		long totaltime = 0;
		String mobiles = "15023286425";
		String smscontent = "若有疑问，请随时与我们联系！";
		HttpURLConnection connection = null;
		try {
			String url = UrlConst.sendSms;
			URL postUrl = new URL(url);
			// 打开连接
			long totalt1 = System.currentTimeMillis();
			connection = (HttpURLConnection) postUrl.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.setInstanceFollowRedirects(true);
			connection.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");
			connection.connect();
			DataOutputStream out = new DataOutputStream(
					connection.getOutputStream());
			String timestamp = Long.toString(System.currentTimeMillis());
			StringBuffer access_token = new StringBuffer();
			access_token.append(timestamp).append(password);
			String access_tokenS = MD5.md5(access_token.toString());

			StringBuffer s1 = new StringBuffer();
			s1.append("account=").append(account).append("&timestamp=")
					.append(timestamp);
			s1.append("&access_token=").append(access_tokenS)
					.append("&receiver=").append(mobiles)
					.append("&smscontent=")
					//.append(Base64.encode(smscontent.getBytes("GBK")))
					.append(java.net.URLEncoder.encode(smscontent,"UTF-8"))
					.append("&extcode=");
			out.writeBytes(s1.toString());
			out.flush();
			out.close();
			long totalt2 = System.currentTimeMillis();
			totaltime = totaltime + (totalt2 - totalt1);
			System.out.println(connection.getResponseCode()
					+ "-----------------");
			respCode=connection.getResponseCode();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					connection.getInputStream(), "UTF-8"));// 设置编码,否则中文乱码
			String lines;
			while ((lines = reader.readLine()) != null) {
				JSONObject jsonOs = JSONObject.parseObject(lines);
				for (Object setkeyS : jsonOs.keySet()) {
					System.out.println(setkeyS + ":" + jsonOs.get(setkeyS));
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (connection != null) {
				connection.disconnect();
			}
		}
		long t2 = System.currentTimeMillis();
		System.out.println("耗时......" + totaltime);
		System.out.println("耗时,包括取连接......" + (t2 - t1));
		assertEquals(respCode, 200);
	}
	
	@Test
	public void querySmsStatus() {
		int respCode=0;
		long t1 = System.currentTimeMillis();
		long totaltime = 0;
		String identifier="89033";
		String mobiles = "";
		HttpURLConnection connection = null;
		try {
			String url = UrlConst.querySmsStatus;
			URL postUrl = new URL(url);
			// 打开连接
			long totalt1 = System.currentTimeMillis();
			connection = (HttpURLConnection) postUrl.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.setInstanceFollowRedirects(true);
			connection.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");
			connection.connect();
			DataOutputStream out = new DataOutputStream(
					connection.getOutputStream());
			String timestamp = Long.toString(System.currentTimeMillis());
			StringBuffer access_token = new StringBuffer();
			access_token.append(timestamp).append(password);
			String access_tokenS = MD5.md5(access_token.toString());

			StringBuffer s1 = new StringBuffer();
			s1.append("account=").append(account).append("&timestamp=")
					.append(timestamp);
			s1.append("&access_token=").append(access_tokenS)
					.append("&identifier=").append(identifier)
					.append("&msisdn=")
					.append(mobiles);
			out.writeBytes(s1.toString());
			out.flush();
			out.close();
			long totalt2 = System.currentTimeMillis();
			totaltime = totaltime + (totalt2 - totalt1);
			System.out.println(connection.getResponseCode()
					+ "-----------------");
			respCode=connection.getResponseCode();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					connection.getInputStream(), "UTF-8"));// 设置编码,否则中文乱码
			String lines;
			while ((lines = reader.readLine()) != null) {
				JSONObject jsonOs = JSONObject.parseObject(lines);
				for (Object setkeyS : jsonOs.keySet()) {
					System.out.println(setkeyS + ":" + jsonOs.get(setkeyS));
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (connection != null) {
				connection.disconnect();
			}
		}
		long t2 = System.currentTimeMillis();
		System.out.println("耗时......" + totaltime);
		System.out.println("耗时,包括取连接......" + (t2 - t1));
		assertEquals(respCode, 200);
	}
	
	@Test
	public void queryMo() {
		int respCode=0;
		long t1 = System.currentTimeMillis();
		long totaltime = 0;
		HttpURLConnection connection = null;
		try {
			String url = UrlConst.queryMo;
			URL postUrl = new URL(url);
			// 打开连接
			long totalt1 = System.currentTimeMillis();
			connection = (HttpURLConnection) postUrl.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.setInstanceFollowRedirects(true);
			connection.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");
			connection.connect();
			DataOutputStream out = new DataOutputStream(
					connection.getOutputStream());
			String timestamp = Long.toString(System.currentTimeMillis());
			StringBuffer access_token = new StringBuffer();
			access_token.append(timestamp).append(password);
			String access_tokenS = MD5.md5(access_token.toString());

			StringBuffer s1 = new StringBuffer();
			s1.append("account=").append(account).append("&timestamp=")
					.append(timestamp);
			s1.append("&access_token=").append(access_tokenS);
			out.writeBytes(s1.toString());
			out.flush();
			out.close();
			long totalt2 = System.currentTimeMillis();
			totaltime = totaltime + (totalt2 - totalt1);
			System.out.println(connection.getResponseCode()
					+ "-----------------");
			respCode=connection.getResponseCode();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					connection.getInputStream(), "UTF-8"));// 设置编码,否则中文乱码
			String lines;
			while ((lines = reader.readLine()) != null) {
				JSONObject jsonOs = JSONObject.parseObject(lines);
				for (Object setkeyS : jsonOs.keySet()) {
					System.out.println(setkeyS + ":" + jsonOs.get(setkeyS));
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (connection != null) {
				connection.disconnect();
			}
		}
		long t2 = System.currentTimeMillis();
		System.out.println("耗时......" + totaltime);
		System.out.println("耗时,包括取连接......" + (t2 - t1));
		assertEquals(respCode, 200);
	}

	@Test
	public void queryBalance() {
		int respCode=0;
		long t1 = System.currentTimeMillis();
		long totaltime = 0;
		HttpURLConnection connection = null;
		try {
			String url = UrlConst.queryBalance;
			URL postUrl = new URL(url);
			// 打开连接
			long totalt1 = System.currentTimeMillis();
			connection = (HttpURLConnection) postUrl.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.setInstanceFollowRedirects(true);
			connection.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");
			connection.connect();
			DataOutputStream out = new DataOutputStream(
					connection.getOutputStream());
			String timestamp = Long.toString(System.currentTimeMillis());
			StringBuffer access_token = new StringBuffer();
			access_token.append(timestamp).append(password);
			String access_tokenS = MD5.md5(access_token.toString());

			StringBuffer s1 = new StringBuffer();
			s1.append("account=").append(account).append("&timestamp=")
					.append(timestamp);
			s1.append("&access_token=").append(access_tokenS);
			out.writeBytes(s1.toString());
			out.flush();
			out.close();
			long totalt2 = System.currentTimeMillis();
			totaltime = totaltime + (totalt2 - totalt1);
			System.out.println(connection.getResponseCode()
					+ "-----------------");
			respCode=connection.getResponseCode();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					connection.getInputStream(), "UTF-8"));// 设置编码,否则中文乱码
			String lines;
			while ((lines = reader.readLine()) != null) {
				JSONObject jsonOs = JSONObject.parseObject(lines);
				for (Object setkeyS : jsonOs.keySet()) {
					System.out.println(setkeyS + ":" + jsonOs.get(setkeyS));
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (connection != null) {
				connection.disconnect();
			}
		}
		long t2 = System.currentTimeMillis();
		System.out.println("耗时......" + totaltime);
		System.out.println("耗时,包括取连接......" + (t2 - t1));
		assertEquals(respCode, 200);
	}
}
